package reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—07—23
 * Time: 15:43
 */
public class ReflectClassDemo {

    //通过反射创建对象
    public static void reflectNewInstance() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");
            Student student = (Student) c1.newInstance();

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // 反射私有的构造方法 屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");
            //注意传入对应的参数
            Constructor<?> constructor =
                    c1.getDeclaredConstructor(String.class, int.class);
            //你确定要在类外访问私有方法吗.设置为true后可修改访问权限
            constructor.setAccessible(true);

            Student student = (Student) constructor.newInstance("xiangyu",22);
            System.out.println(student);

        } catch (ClassNotFoundException e) {

        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    //反射私有属性
    public static void reflectPrivateField() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");
            Student student = (Student) c1.newInstance();
            //可以修改该属性的值
            Field field = c1.getDeclaredField("name");
            //访问私有的，提前询问一下是否确定访问
            field.setAccessible(true);
            field.set(student,"liubang");
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    //反射私有方法
    public static void reflectPrivateMethod() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");
            Student student = (Student) c1.newInstance();

            Method method = c1.getDeclaredMethod("function", String.class);
            method.setAccessible(true);

            method.invoke(student,"通过反射给你传参");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }
}
